package com.easy.component.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.component.R;

/**
 * 描述: {dialog alert}
 * 作者: jack-song
 * 创建日期: 2017/11/7 10:04
 * 修改日期: 2017/11/7
 * 版本: {版本}
 */
public class EasyAlertDialog extends Dialog {
    /**
     * 提示
     */
    private TextView tvTitle;
    /**
     *
     */
    private TextView tvMessage;
    /**
     * 左侧按钮
     */
    private Button btnLeft;
    /**
     * 右侧按钮
     */
    private Button btnRigth;
    /**
     * 提示
     */
    private CharSequence title;
    /**
     *
     */
    private CharSequence message;
    /**
     * 左侧文字
     */
    private CharSequence leftText;
    /**
     * 右侧文字
     */
    private CharSequence rigthText;
    /**
     * 左侧按钮
     */
    private View.OnClickListener leftOnClickListener;
    /**
     * 右侧按钮
     */
    private View.OnClickListener rigthOnClickListener;

    public EasyAlertDialog(@NonNull Context context) {
        super(context);
    }

    public EasyAlertDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.easy_dialog_alert);
        tvTitle = findViewById(R.id.tv_title);
        tvMessage = findViewById(R.id.tv_msg);
        btnLeft = findViewById(R.id.btn_left);
        btnRigth = findViewById(R.id.btn_rigth);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
            tvTitle.setText("");
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        if (!TextUtils.isEmpty(message)) {
            tvMessage.setText(message);
        } else {
            tvMessage.setText("");
        }
        if (null == leftOnClickListener) {
            btnLeft.setVisibility(View.GONE);
        } else {
            btnLeft.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(leftText)) {
                btnLeft.setText(leftText);
            }
            btnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    leftOnClickListener.onClick(view);
                    dismiss();
                }
            });
        }
        if (null == rigthOnClickListener) {
            btnRigth.setVisibility(View.GONE);
        } else {
            btnRigth.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(rigthText)) {
                btnRigth.setText(rigthText);
            }
            btnRigth.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rigthOnClickListener.onClick(view);
                    dismiss();
                }
            });

        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager wm = window.getWindowManager();
        Point windowSize = new Point();
        wm.getDefaultDisplay().getSize(windowSize);
        window.getAttributes().width = (int) (windowSize.x * 0.8f);
        window.getAttributes().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);

    }

    public void setTitle(CharSequence title) {
        this.title = title;
    }

    public void setMessage(CharSequence message) {
        this.message = message;
    }

    public void setLeftText(CharSequence leftText) {
        this.leftText = leftText;
    }

    public void setRigthText(CharSequence rigthText) {
        this.rigthText = rigthText;
    }

    public void setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
        this.leftOnClickListener = leftOnClickListener;
    }

    public void setRigthOnClickListener(View.OnClickListener rigthOnClickListener) {
        this.rigthOnClickListener = rigthOnClickListener;
    }

    public static class Builder {
        /**
         * 上下文
         */
        private Context context;
        /**
         * 提示
         */
        private CharSequence title;
        /**
         * 提示
         */
        private CharSequence message;
        /**
         * 左侧文字
         */
        private CharSequence leftText;
        /**
         * 右侧文字
         */
        private CharSequence rigthText;
        /**
         * 左侧按钮
         */
        private View.OnClickListener leftOnClickListener;
        /**
         * 右侧按钮
         */
        private View.OnClickListener rigthOnClickListener;

        public Builder() {
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setTitle(CharSequence title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }


        public Builder setLeftText(CharSequence leftText) {
            this.leftText = leftText;
            return this;
        }

        public Builder setRigthText(CharSequence rigthText) {
            this.rigthText = rigthText;
            return this;
        }

        public Builder setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
            this.leftOnClickListener = leftOnClickListener;
            return this;
        }

        public Builder setRigthOnClickListener(View.OnClickListener rigthOnClickListener) {
            this.rigthOnClickListener = rigthOnClickListener;
            return this;
        }


        public EasyAlertDialog builder() {
            EasyAlertDialog loadingDialog = new EasyAlertDialog(context, R.style.loading_dialog);
            loadingDialog.setTitle(title);
            loadingDialog.setMessage(message);
            loadingDialog.setLeftText(leftText);
            loadingDialog.setRigthText(rigthText);
            loadingDialog.setLeftOnClickListener(leftOnClickListener);
            loadingDialog.setRigthOnClickListener(rigthOnClickListener);
            return loadingDialog;
        }
    }
}
